import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex)

const store = new Vuex.Store({
	state: {
		nickName: uni.getStorageSync('nickName'),
		loginState: uni.getStorageSync('loginState') === 'ok' ? true : false,
	},
	mutations: {
		MLOGIN(state, nickName) {
			state.nickName = nickName
			state.loginState = true
			uni.setStorageSync('loginState', 'ok')
			uni.setStorageSync('nickName', nickName)
		},
		MLOGOUT(state) {
			state.nickName = '未登录用户'
			state.loginState = false
			uni.clearStorageSync('loginState')
			uni.clearStorageSync('nickName')
			uni.clearStorageSync('token')
		}
	},
	actions: {
		loginAction(context, nickName) {
			context.commit('MLOGIN', nickName)
		},
		logoutAction(context) {
			context.commit('MLOGOUT')
		}
	}
})

export default store
